/*
 *************************************************************************
 *西瑞克斯研发管理系统
 *版 本: 1.0   
 *日 期：02/08/2012    
 *作 者： 王浩 QQ：873987060   
 *************************************************************************   
 */
package com.application.transition.struts.actions;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.manager.commons.config.ConfigFactory;
import com.manager.commons.page.PageBean;
import com.manager.commons.CurrentUserUtils;
import com.manager.commons.RandomKey;
import com.manager.commons.CommonUtil;
import com.application.attachment.entity.Attachment;
import com.application.attachment.service.AttachmentService;
import com.application.attachment.struts.form.AttachmentForm;
import com.application.mac.struts.form.MacForm;
import com.application.role.entity.Role;
import com.application.transition.entity.Transition;
import com.application.transition.struts.form.TransitionForm;
import com.application.transition.service.TransitionService;
import com.application.transition.commons.Constants;
import com.application.usertable.entity.Usertable;

/**
 * 
 * Description:TransitionAction<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060 <br />
 * 
 * History: <br />
 * 
 * 02/08/2012
 * 
 */
public class TransitionAction extends DispatchAction {

	/**
	 * Service层实例
	 */
	private TransitionService transitionService;
	private AttachmentService attachmentService;

	/**
	 * 日志
	 */
	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * 实体对象
	 */
	private Transition transition;

	/**
	 * 采用partialList方式分页程序
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getTransitionPartialList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("searchTransition" + "开始");
		TransitionForm transition = (TransitionForm) form;
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List transitionList = null;
		resultSize = transitionService.getAllCountTransition(transition);
		pageBean = PageBean.getPageBean("transitionTab", resultSize, request);
		transitionList = transitionService.searchTransition(transition,
				pageBean);
		request.setAttribute("transitionList", transitionList);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		String addQx = "noAdd";
		Usertable user = CurrentUserUtils.getCurrentUser();
		List<Role> role = user.getRoleList();
		for (int i = 0; i < role.size(); i++) {
			if (role.get(i).getName().equals("研发经理")
					|| role.get(i).getName().equals("研发中心负责人")
					|| role.get(i).getName().equals("分管副总")
					|| role.get(i).getName().equals("总经理")) {
				addQx = "Add";
			}
		}
		request.setAttribute("addQx", addQx);
		log.debug("searchTransition" + "结束");

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达新增页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toAddTransition(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("toAddTransitions");
		TransitionForm transitionForm = new TransitionForm();
		transitionForm.setId(RandomKey.generateID_Long_19());
		transitionForm.setCreatedate(new Date());
		request.setAttribute("transitionForm", transitionForm);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 增加
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward addTransition(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("addTransitions");
		Transition transition = this.getTransition(form);
		transition.setCreatedate(new Date());
		this.transitionService.insertObject(transition);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 删除
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward deleteTransition(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("deleteTransitions");
		TransitionForm transitionForm = (TransitionForm) form;
		this.transitionService.deleteTransitions(transitionForm);
		AttachmentForm attachmentForm = new AttachmentForm();
		attachmentForm.setFid(transitionForm.getId().toString());
		attachmentForm.setType(com.manager.commons.Constants.FJ_TYPE_XMDY);
		List<Attachment> attachment = attachmentService
				.queryAttachment(attachmentForm);
		String serverPath = CommonUtil.ATTACHE_FILE;
		if (StringUtils.isBlank(serverPath)) {
			serverPath = request.getSession().getServletContext().getRealPath(
					"/");
		}
		if (attachment != null && attachment.size() > 0) {
			for (Attachment attachment2 : attachment) {
				File file = new File(serverPath + attachment2.getUrl());
				file.delete();
				attachmentService.deleteObject(attachment2);

			}
		}
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达更新页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toUpdateTransition(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("toUpdateTransitions");
		TransitionForm transitionForm = (TransitionForm) form;
		transition = (Transition) this.transitionService.searchTransition(
				transitionForm).get(0);
		request.setAttribute(Constants.DEPARTMENT, transition);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 更新
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward updateTransition(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("updateTransitions");
		Transition transition = this.getTransition(form);
		transition.setCreatedate(new Date());
		this.transitionService.updateObject(transition);

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 查看页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward viewTransition(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("viewTransition");
		List<Attachment> attachList = null;
		TransitionForm transitionForm = (TransitionForm) form;
		HttpSession session = request.getSession();
		MacForm mac = (MacForm) session.getAttribute("mac");
		transition = (Transition) this.transitionService.searchTransition(
				transitionForm).get(0);
		String projectNo = transition.getProjectNo();
		if (StringUtils.isNotBlank(projectNo)) {
			AttachmentForm attachmentForm = new AttachmentForm();
			attachmentForm.setBak1(projectNo);
			attachmentForm
					.setInclude("'12005','12004','30002','30003','30004','30005','30006'");
			attachList = attachmentService.queryAttachment(attachmentForm);

		}
		showAttachList(attachList, request);
		if (attachList == null) {
			attachList = new ArrayList();
		}
		String delQx = "noDel";
		String xzQx = "noXz";
		Usertable user = CurrentUserUtils.getCurrentUser();
		List<Role> role = user.getRoleList();
		int a = user.getOid().trim().compareTo(
				transition.getProjectorg().trim());
		for (int i = 0; i < role.size(); i++) {
			if (role.get(i).getName().equals("研发经理") && a == 0) {
				delQx = "Del";
			}
			if (role.get(i).getName().equals("研发经理")
					|| role.get(i).getName().equals("研发中心负责人")
					|| role.get(i).getName().equals("分管副总")
					|| role.get(i).getName().equals("总经理")) {

				if (mac != null && mac.getSpecial() != null
						&& mac.getSpecial().equals("下载特权")) {
					xzQx = "Xz";
				}

			}
		}
		request.setAttribute("delQx", delQx);
		request.setAttribute("xzQx", xzQx);
		request.setAttribute(Constants.DEPARTMENT, transition);
		return mapping.findForward(Constants.SUCCESS);
	}

	private void showAttachList(List<Attachment> attachList,
			HttpServletRequest request) {
		List<Attachment> rjzxt = new ArrayList<Attachment>();
		List<Attachment> yjylt = new ArrayList<Attachment>();
		List<Attachment> yjpcb = new ArrayList<Attachment>();
		List<Attachment> bom = new ArrayList<Attachment>();
		List<Attachment> rjb = new ArrayList<Attachment>();
		request.setAttribute("rjzxt", rjzxt);
		request.setAttribute("yjylt", yjylt);
		request.setAttribute("yjpcb", yjpcb);
		request.setAttribute("bom", bom);
		request.setAttribute("rjb", rjb);
		for (Attachment attachment : attachList) {

			if (attachment.getType().equals(
					com.manager.commons.Constants.FJ_TYPE_RJZXTDM)) { // 软件子系统代码
				rjzxt.add(attachment);
			}
			if (attachment.getType().equals(
					com.manager.commons.Constants.FJ_TYPE_YJYLT)) { // 硬件原理图
				yjylt.add(attachment);
			}
			if (attachment.getType().equals(
					com.manager.commons.Constants.FJ_TYPE_PCB)) { // 硬件PCB
				yjpcb.add(attachment);
			}
			if (attachment.getType().equals(
					com.manager.commons.Constants.FJ_TYPE_BOM)) { // BOM清单
				bom.add(attachment);
			}
			if (attachment.getType().equals(
					com.manager.commons.Constants.FJ_TYPE_RJB)) { // 软件包
				rjb.add(attachment);
			}
		}
	}

	// get--set
	/**
	 * @return the TransitionService
	 */
	public TransitionService getTransitionService() {
		return this.transitionService;
	}

	/**
	 * @param TransitionService
	 *            the TransitionService to set
	 */
	public void setTransitionService(TransitionService transitionService) {
		this.transitionService = transitionService;
	}

	/**
	 * 获取Transition实体
	 * 
	 * @param ActionForm
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @return the Transition
	 */
	public Transition getTransition(ActionForm form)
			throws IllegalAccessException, InvocationTargetException {
		TransitionForm transitionForm = (TransitionForm) form;
		transition = new Transition();
		BeanUtils.copyProperties(transition, transitionForm);
		return transition;
	}

	public void setTransition(Transition transition) {
		this.transition = transition;
	}

	/**
	 * @return the attachmentService
	 */
	public AttachmentService getAttachmentService() {
		return attachmentService;
	}

	/**
	 * @param attachmentService
	 *            the attachmentService to set
	 */
	public void setAttachmentService(AttachmentService attachmentService) {
		this.attachmentService = attachmentService;
	}

	/**
	 * @return the transition
	 */
	public Transition getTransition() {
		return transition;
	}

}
